package com.googlecode.perfectable.eventable.routing;

import static com.google.common.base.Preconditions.checkNotNull;

import java.util.Collection;
import java.util.HashSet;

public abstract class BaseSource<M> implements Source<M> {
	private Collection<Sink<M>> listeners = new HashSet<>();

	@Override
	public void listen(Sink<M> sink) {
		checkNotNull(sink);
		this.listeners.add(sink);
	}

	@Override
	public void stop(Sink<M> sink) {
		checkNotNull(sink);
		this.listeners.remove(sink);
	}

	protected final void publish(M element) {
		for(Sink<M> listener : this.listeners) {
			listener.receive(element);
		}
	}

}
